package cn.edu.ctbu.websql.service;

import cn.edu.ctbu.websql.domain.Stscore;
import cn.edu.ctbu.websql.repository.StscoreRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public class StscoreService {
    @Autowired
    private StscoreRepository stscoreRepository;
    public Page<Stscore> getAll(Pageable pageable){
        return stscoreRepository.findAll(pageable);
    }
    public List<Stscore> findAll(){
        return stscoreRepository.findAll();
    }
    public Optional<Stscore> getData(Integer id){
        return stscoreRepository.findById(id);
    }
    public Stscore update(Integer id,Stscore newStscore){
        Optional<Stscore> stscoreOptional = stscoreRepository.findById(id);
        if(stscoreOptional.isPresent()){

            Stscore stscore = stscoreOptional.get();

            stscore.setGaoshu(newStscore.getGaoshu());
            stscore.setXiandai(newStscore.getXiandai());
            stscore.setJizu(newStscore.getJizu());

            return stscoreRepository.save(stscore);
        }
        return null;
    }

    public List<Stscore> searchData(String no){
        return stscoreRepository.findAllByNoLike(no+"%");
    }



}

